//
//  AppDelegate.m
//  InduceTool
//
//  Created by 魏延龙 on 2020/8/3.
//  Copyright © 2020 魏延龙. All rights reserved.
//

#import "AppDelegate.h"
#import "ITReplacingResourceURLProtocol.h"
#import <LEETheme/LEETheme.h>
#import "ITWKWebViewController.h"

#ifdef DEBUG
#import <DoraemonKit/DoraemonManager.h>
#import "ITPluginViewController.h"
#endif

typedef void(^CompletionHandlerType)();

@interface AppDelegate ()

@property(nonatomic, copy) CompletionHandlerType handler;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    NSLog(@"didFinishLaunchingWithOptions %f", application.backgroundTimeRemaining);
    
    #ifdef DEBUG
    [self configDoraemonKit];
    #endif
    
    [self configTheme];
    return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // 可以通过这个值判断是否被动启动
    NSLog(@"applicationDidBecomeActive %f", application.backgroundTimeRemaining);
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"applicationDidEnterBackground  %f", application.backgroundTimeRemaining);
}

//配置Doraemon工具集
- (void)configDoraemonKit{
    [[DoraemonManager shareInstance] addPluginWithTitle:@"环境切换" icon:@"doraemon_default" desc:@"用于app内部环境切换功能" pluginName:@"ITPluginViewController" atModule:@"业务专区" handle:^(NSDictionary * _Nonnull itemData) {
        UINavigationController *navi = (UINavigationController *)self.window.rootViewController;
        [navi pushViewController:[ITPluginViewController new] animated:YES];
    }];
    [[DoraemonManager shareInstance] installWithPid:@"productId"];
}

// 配置主题
- (void)configTheme {
    NSString *grayJsonPath = [[NSBundle mainBundle] pathForResource:@"tag_gray_json" ofType:@"json"];
    NSString *greenJsonPath = [[NSBundle mainBundle] pathForResource:@"tag_green_json" ofType:@"json"];
    NSString *redJsonPath = [[NSBundle mainBundle] pathForResource:@"tag_red_json" ofType:@"json"];
    NSString *grayJson = [NSString stringWithContentsOfFile:grayJsonPath encoding:NSUTF8StringEncoding error:nil];
    NSString *greenJson = [NSString stringWithContentsOfFile:greenJsonPath encoding:NSUTF8StringEncoding error:nil];
    NSString *redJson = [NSString stringWithContentsOfFile:redJsonPath encoding:NSUTF8StringEncoding error:nil];
    [LEETheme defaultTheme:@"gray"];
    [LEETheme addThemeConfigWithJson:grayJson Tag:@"gray" ResourcesPath:nil];
    [LEETheme addThemeConfigWithJson:greenJson Tag:@"green" ResourcesPath:nil];
    [LEETheme addThemeConfigWithJson:redJson Tag:@"red" ResourcesPath:nil];
}

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(nonnull NSString *)identifier completionHandler:(nonnull void (^)(void))completionHandler{
//    self.handler = completionHandler;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    NSLog(@"application openURL %@", url);
    UINavigationController *navi = (UINavigationController *)self.window.rootViewController;
    if ([url.absoluteString isEqualToString:@"todaytool://wkwebview"] && ![navi.viewControllers.lastObject isKindOfClass:[ITWKWebViewController class]]) {
        [navi pushViewController:[ITWKWebViewController new] animated:YES];
    }
    return YES;
}

@end
